package com.editso.haier.result;

import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.data.domain.Page;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

@Data
@AllArgsConstructor
public class PageData<T> {
    private Long total;
    private Integer pageTotal;
    private Integer pageSize;
    private Long currentPage;
    private Integer itemTotal;
    private List<T> items;

    public static <T,R> PageData<R> from(Page<T> page, Function<T,R> function){
        return new PageData<>(
                page.getTotalElements(),
                page.getTotalPages(),
                page.getSize(),
                page.getPageable().getOffset() / page.getSize(),
                page.getNumberOfElements(),
                page.stream().map(function).collect(Collectors.toList())
        );
    }
}
